<?php
/**
 * 项目：红河移动电路资源管理信息系统
 *
 * 公司：红河移动分公司
 *
 * @author 李朝应
 *
 * E-mail:lichaoying.hh@163.com
 *
 */
class Level_model extends CI_Model {
   function __construct()
    {
        parent::__construct();
    }


   function setLev($name,$dspt,$lev){
    $data = array(
               'level_name' => $name,
               'level_description' => $dspt,
               'level' => $lev,
            );

    $this->db->insert('level', $data);
    return $this->db->affected_rows() ;

   }
 function updateLev($id,$name,$dspt,$lev){
    $data = array(
               'level_name' => $name,
               'level_description' => $dspt,
               'level' => $lev,
            );
      $this->db->where('level_id',$id);
      $this->db->update('level', $data);
    return $this->db->affected_rows() ;
 }
function delLev($id){
    $this->db->delete('level', array('level_id' => $id));
	return true;
 }

function getCount(){
      return  $this->db->count_all("level");
   }

 function getAll(){
     $query = $this->db->get('level');
		if ($query->num_rows() > 0)
		{
			  return $query; //返回查询结果
        }
   }
    function getLev($id){
     $this->db->where("level_id", $id);
     $query = $this->db->get('level');
		if ($query->num_rows() > 0)
		{
			 return $query->row_array(); //返回查询结果
        }
   }
   function hasSameLev($name,$lev){
        $this->db->where("level_name", $name);
        $this->db->where("level", $lev);
		$query = $this->db->get('level');
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
   }
  function hasSameRec($id,$name,$lev){
        $this->db->where("level_id  !=", $id);
        $this->db->where("level_name ", $name);
        $this->db->where("level ", $lev);
		$query = $this->db->get('level ');
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
   }
 function getLevOpt(){
     $this->db->select("level_id as id ,level_name as name,level as lev");
	 $query = $this->db->get('level');
     $oPt="";
     foreach ($query->result_array() as $row) {
         $oPt .= "<input  type='checkbox' name='ck".$row['id']."' id='ck".$row['id']."' value='".$row['lev' ]."'>&nbsp;&nbsp;<span>".$row['name']."</span><br>";
         }
        return  $oPt;
 }
 function getLevUpdateOpt($id){
     //取得$id的级别字符串
     $str="";
     $this->db->where('us_id',$id);
     $this->db->select("level_str as lev");
     $query = $this->db->get('user');
     foreach ($query->result() as $row){
       $str=$row->lev;
      }
      //取得所有级别记录
     $this->db->select("level_id as id ,level_name as name,level as lev");
	 $query = $this->db->get('level');
     $oPt="";
     foreach ($query->result_array() as $row) {
         if(strstr($str,$row['lev' ])): //判断是否属于级别字符串是否属于子串
            $oPt .= "<input  type='checkbox' checked name='ck".$row['id']."' id='ck".$row['id']."' value='".$row['lev' ]."'>&nbsp;&nbsp;<span>".$row['name']."</span><br>";//选中此oPt.
         else:
            $oPt .= "<input  type='checkbox' name='ck".$row['id']."' id='ck".$row['id']."' value='".$row['lev' ]."'>&nbsp;&nbsp;<span>".$row['name']."</span><br>";
         endif;
     }
        return  $oPt;
 }
 function hasLev($login,$str){
    //取得$login的级别字符串
     $this->db->where('us_login',$login);
     $this->db->select("level_str as lev");
     $query = $this->db->get('user');
     foreach ($query->result() as $row){
       $user_level=$row->lev;
      }
     //判断是否属于级别字符串是否属于子串
     if(strstr($user_level,$str)){
         return true;
     }else{
         return false;
     }
 }
}//类定义结束
?>
